home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: freeing structures
- Date: 7 Feb 1996 08:54:41 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4f9pch$9dp@news.iag.net>
- References: <4f7913$brg@sepia.nioz.nl>
- NNTP-Posting-Host: pm1-orl17.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4f7913$brg@sepia.nioz.nl>, rikko@nioz.nl says...
- <snip>
- >typedef struct Line {
- >
- > struct Line *next; /* Pointer to next item */
- > char **dpart[3];
- > char **DateLine[3]; /* contains the date in form yy-mm-dd */
-
- dpart is defined as an array of 3 pointers to pointer to char
- DateLine has a matching definition
-
- This is perfectly legal and may be quite valid in your algorithm. However,
- considering the way you are trying to free your memory, it looks a bit odd.
-
- > char *RestLine; /* list of the Values of the line */
- >} Line;
- >
- <snip>
- >void ClearLines()
- >{
- >Line* line;
- <snip>
- > free(line->dpart[0]);
-
- Ok you have freed pointer(s) that this pointed to. Did you need to free the
- char(s) that were pointed to by the pointer(s) first?
-
- > free(line->dpart[1]);
- > free(line->dpart[2]);
- > free(line->DateLine[0]);
- > free(line->DateLine[1]);
- > free(line->DateLine[2]);
- <snip>
- > free(*line->dpart);
- > free(*line->DateLine);
-
- *line->dpart is a different form of line->dpart[0] <aka: *(line->dpart + 0)>
-
- Ditto *line->DateLine and line->DateLine[0]
-
- In both cases you are trying to free pointers that have already beeen freed.
-
- >
- > /*
- > * delete the line itself and let 'line' point to the next
- line
- > */
- >
- > free(&line); /* I suspect this line to trigger the error */
- <snip>
-
- line is an object of automatic storage class (ie it is automatically
- allocated at the start of the function and destroyed at the end). By passing
- its address to free, you are attempting to free an automatic object. Not a
- good idea.
-
- I suspect you meant to free the dynamic (I hope?) object pointed to by line.
- In this case pass the contents of line (a pointer to a Line object) to free.
-
- free(line);
-
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-